#!/bin/bash -e
#
# ===----------------------------------------------------------------------===
#
#  Swift Android SDK: Local (non-Docker-containerized) Build Script
#
#  This source file is part of the Swift.org open source project
#
#  Copyright (c) 2025 Apple Inc. and the Swift project authors
#  Licensed under Apache License v2.0 with Runtime Library Exception
#
#  See https://swift.org/LICENSE.txt for license information
#  See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ===----------------------------------------------------------------------===

# default architectures to build for
TARGET_ARCHS=${TARGET_ARCHS:-aarch64,x86_64,armv7}

ANDROID_NDK_VERSION=android-ndk-r27d
ANDROID_API=28

BASEPATH=$(dirname $(realpath $0))
cd ${BASEPATH}

export SWIFT_VERSION=${1}
# note that WORKDIR must not be under the current checkout or the patches will fail to apply
WORKDIR=${2}
if [[ "${WORKDIR}" == '' ]]; then
    echo "Usage: $(basename $0) <release/devel/trunk> <work directory>"
    exit 1
fi
mkdir -p ${WORKDIR}
WORKDIR=$(realpath ${WORKDIR})

HOST_OS=ubuntu$(lsb_release -sr)
source ./scripts/toolchain-vars.sh

SWIFT_ROOT=${WORKDIR}/host-toolchain
HOST_TOOLCHAIN=$SWIFT_ROOT/$SWIFT_BASE/usr
if [[ ! -d "$HOST_TOOLCHAIN" ]]; then
    ./scripts/install-swift.sh ${HOST_TOOLCHAIN}
fi

$HOST_TOOLCHAIN/bin/swift --version

# ensure the correct Swift is first in the PATH
export PATH=$HOST_TOOLCHAIN/bin:$PATH

export ANDROID_NDK_HOME=${WORKDIR}/ndk/${ANDROID_NDK_VERSION}

if [[ ! -d ${ANDROID_NDK_HOME} ]]; then
    mkdir -p $(dirname ${ANDROID_NDK_HOME})
    pushd $(dirname ${ANDROID_NDK_HOME})
    NDKFILE=$(basename $ANDROID_NDK_HOME)-linux.zip
    wget -q https://dl.google.com/android/repository/${NDKFILE}
    unzip -q ${NDKFILE}
    popd
fi


# Check-out and patch the sources
./scripts/fetch-source.sh --source-dir ${WORKDIR}/source
# This `git grep` invocation in a trunk test fails in our Docker for some
# reason, so just turn it into a plain `grep` again.
perl -pi -e 's:"git",:#:' ${WORKDIR}/source/swift-project/swift/test/Misc/verify-swift-feature-testing.test-sh

# Work around swiftlang/swift-driver#1822 for now
perl -pi -g -we "s#(call rm ... \".\{LIBDISPATCH_BUILD_DIR\}\"\n(\s+)fi\n)#\1\2if [[ -d \"\\\${ANDROID_NDK}\" ]]; then call ln -sf \"\\\${SWIFT_BUILD_PATH}/lib/swift\" \"\\\${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib\"; fi#" ${WORKDIR}/source/swift-project/swift/utils/build-script-impl

# disable backtrace() for Android (needs either API33+ or libandroid-execinfo, or to manually add in backtrace backport)
perl -pi -e 's;os\(Android\);os\(AndroidDISABLED\);g' ${WORKDIR}/source/swift-project/swift-testing/Sources/Testing/SourceAttribution/Backtrace.swift

mkdir -p ${WORKDIR}/products

./scripts/build.sh \
    --source-dir "${WORKDIR}/source" \
    --products-dir "${WORKDIR}/products" \
    --build-dir "${WORKDIR}/build" \
    --build-compiler "${BUILD_COMPILER}" \
    --host-toolchain "${HOST_TOOLCHAIN}" \
    --android-api "${ANDROID_API}" \
    --ndk-home "${ANDROID_NDK_HOME}" \
    --archs "${TARGET_ARCHS}"
